function request(obj,callback){
  var url = obj.url,
      method = obj.method,
      async,
      data,
      header;
  if (obj.async) {
    async = obj.async;
  } else {
    async = true;
  }
  
  if (obj.data) {
    data = assistant.obj_to_str(obj.data);
  } else {
    data = null;
  }
  if (obj.header) {
    header = obj.header;
  } else {
    header = null;
  }

  var xmlhttp = new XMLHttpRequest() || new ActiveXObject("Microsoft.XMLHTTP");
  xmlhttp.open(method, url, async);
  if (header) {
    for (var key in header) {
      xmlhttp.setRequestHeader(key, header[key]);
    }
  }
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Accept", "*/*");
  xmlhttp.onreadystatechange = () => {
    if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
        var res_type = xmlhttp.getResponseHeader("Content-Type");
        //_this.success(JSON.parse(xmlhttp.response));
        if (res_type.match("text/xml") != null) {
          callback(null,xmlhttp.response);
          return 0;
        } else if (res_type.match("text/json") != null) {
          callback(null,JSON.parse(xmlhttp.responseText));
          return 0;
        } else if (res_type.match("text/html") != null) {
          callback(null,xmlhttp.responseText);
          return 0;
        }
        callback(null,JSON.parse(xmlhttp.responseText));
      } else {
        callback(true,JSON.parse(xmlhttp.responseText));
      }
    }
  };
  xmlhttp.send(data);
}
let assistant = {
  ajax(obj) { //callback accept two params , err and res
    return new Promise(function(resolve,reject){
      request(obj,function(err,res){
        if(err){
          reject(res);
        }else{
          resolve(res);
        }
      })
    })
  },
  
  $(id) {
    return document.querySelector(id);
  },
  $$(idorclass) {
    return document.querySelectorAll(idorclass);
  },
  getCookie() {
    var cookie = {};
    var cookies = document.cookie.split("; ");
    for (var i = 0; i < cookies.length; i++) {
      var onecookie = cookies[i].split("=");
      cookie[onecookie[0]] = onecookie[1];
    }
    return cookie;
  },
  obj_to_str(obj) {
    var data = "";
    for (var key in obj) {
      str = key + "=" + obj[key];
      data += str + "&";
    }
    return data;
  },
  eleAddEvent(element, method, func, mode) {
    if (document.addEventListener) {
      element.addEventListener(method, func, mode);
    } else {
      element.attachEvent(method, func, mode);
    }
  },
  elesAddEvent(elements, method, func, mode) {
    for (var i = 0; i < elements.length; i++) {
      assistant.eleAddEvent(elements[i], method, func, mode);
    }
  },
  addClass(node, classStr) {
    if(node.length){
      for(var i=0;i<node.length;i++){
        node[i].classList.add(classStr);
      }
      return;
    }
    node.classList.add(classStr);
  },
  removeClass(node, classStr) {
    if(node.length){
      for(var i=0;i<node.length;i++){
        node[i].classList.remove(classStr);
      }
      return;
    }
    node.classList.remove(classStr);
  },
  toggleClass(node, class1, class2) {
    assistant.addClass(node, class1);
    assistant.removeClass(node, class2);
  },
  show(node) {
    node.style.display = 'block';
  },
  hide(node) {
    node.style.display = 'none';
  },

  
  //关于dataUrl,blob,file,createObjectURL,canvas等相互转化



  //模板渲染
  render(template, obj) {
    var result = template;
    var reg0 = /\{([^\{\}]+)\}/g;
    var matchArr = template.match(reg0);
    for (var i = 0; i < matchArr.length; i++) {
      var matchStr = matchArr[i].substr(1, matchArr[i].length - 2);
      var indexStrArr = matchStr.split(".");
      var now = obj[indexStrArr[0]];
      for (var j = 1; j < indexStrArr.length; j++) {
        now = now[indexStrArr[j]];
      }
      result = result.replace(matchArr[i], now);
    }
    return result;
  }

}

export const tool = assistant;